package com.baizhi.action;

import com.baizhi.entity.Ultraman;
import com.baizhi.service.UltramanService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Scope("prototype")
public class UltramanAction {
    @Autowired
    private UltramanService ultramanService;
    private HttpServletResponse response = ServletActionContext.getResponse();
    private Map<String, Object> map = new HashMap<>();
    private ObjectMapper mapper = new ObjectMapper();

    private Integer pageNum;
    private Integer pageSize;
    private Ultraman ultraman;
    private int id;
    //展示所有奥特曼信息
    public void show() throws IOException {
        System.out.println("jinruaction");
        List<Ultraman> ultramen = ultramanService.selectUltraman(pageNum, pageSize);
        PageInfo<Ultraman> pageInfo = new PageInfo<>(ultramen, 3);
        int[] pages = pageInfo.getNavigatepageNums();
        response.setCharacterEncoding("utf-8");
        PrintWriter pw = response.getWriter();
        map.put("ultramen",ultramen);
        map.put("pages",pages);
        String s = mapper.writeValueAsString(map);
        pw.print(s);
        pw.flush();
    }
    //添加奥特曼
    public void insertUltraman() throws IOException {
        System.out.println("进来了");
        ultramanService.insertUltraman(ultraman);
        System.out.println("执行了添加");
        map.put("status","success");
        response.setCharacterEncoding("utf-8");
        PrintWriter pw = response.getWriter();
        String s = mapper.writeValueAsString(map);
        pw.print(s);
        pw.flush();
    }
    //删除奥特曼
    public void deleteUltraman() throws IOException {
        try{
            ultramanService.deleteUltramanById(id);
            map.put("status","success");
        }catch (Exception e){
            e.printStackTrace();
            map.put("status", "failed");
        }
        response.setCharacterEncoding("utf-8");
        PrintWriter pw = response.getWriter();
        String s = mapper.writeValueAsString(map);
        pw.print(s);
        pw.flush();
    }
    //修改奥特曼信息
    public void showOne() throws IOException {
        Ultraman ultraman = ultramanService.selectUltramanById(id);
        map.put("ultraman",ultraman);
        response.setCharacterEncoding("utf-8");
        PrintWriter pw = response.getWriter();
        String s = mapper.writeValueAsString(map);
        pw.print(s);
        pw.flush();
    }
    public void updateUltraman() throws IOException {
        ultramanService.updateUltraman(ultraman);
        map.put("status","success");
        response.setCharacterEncoding("utf-8");
        PrintWriter pw = response.getWriter();
        String s = mapper.writeValueAsString(map);
        pw.print(s);
        pw.flush();
    }
    public Integer getPageNum() {
        return pageNum;
    }
    public void setPageNum(Integer pageNum) {
        this.pageNum = pageNum;
    }
    public Integer getPageSize() {
        return pageSize;
    }
    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }
    public Ultraman getUltraman() {
        return ultraman;
    }
    public void setUltraman(Ultraman ultraman) {
        this.ultraman = ultraman;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
}